package top.lishuoboy.boot24.task.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lishuoboy.boot24.task.service.AsyncService;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * @author lishuoboy
 * @date 2022/3/23 14:01
 */
@Slf4j
@RestController
public class TaskController {
    @Autowired
    AsyncService asyncService;
    @Autowired
    JavaMailSender javaMailSender;

    @GetMapping("async")
    private String async() {
        asyncService.sendMsg();
        return "发送成功";
    }

    @SneakyThrows
    @GetMapping("javaMailSender")
    private void javaMailSender(@Value("${spring.mail.username}") String from) {
        // 1、发送简单邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        log.debug("simpleMailMessage=={}", simpleMailMessage);  // simpleMailMessage==SimpleMailMessage: from=null; replyTo=null; to=; cc=; bcc=; sentDate=null; subject=null; text=null
        simpleMailMessage.setFrom(from);                    // 发件人
        simpleMailMessage.setTo("2513539439@qq.com", from); // 收件人们
        simpleMailMessage.setSubject("这是主题1");
        simpleMailMessage.setText("<h1 style='color:red'>这是内容1</h1>");     // 不支持html
        simpleMailMessage.setSentDate(new Date());          // 发送日期
        simpleMailMessage.setCc("2513539439@qq.com");       // 抄送们
        simpleMailMessage.setBcc("2513539439@qq.com");      // 密送们
        simpleMailMessage.setReplyTo("2513539439@qq.com");  // 回复
        javaMailSender.send(simpleMailMessage);


        // 2、发送 MIME 支持html 和 附件
        // (Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // helper包装一层，方便使用。multipart：true支持附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        log.debug("helper=={}", helper);  // helper==SimpleMailMessage: from=null; replyTo=null; to=; cc=; bcc=; sentDate=null; subject=null; text=null
        helper.setFrom(from);                    // 发件人
        helper.setTo(new String[]{"2513539439@qq.com", from});      // 收件人
        helper.setSubject("这是主题2");
        helper.setText("<h1 style='color:red'>这是主题2</h1>", true);     // 支持html
        helper.setText("application/json", "<h1 style='color:red'>这是内容2</h1>");     // html
        File attachment = new File(ResourceUtil.getResource("img.png").getFile());
        helper.addAttachment("img.png", attachment);
        helper.setSentDate(new Date());          // 发送日期
        helper.setCc("2513539439@qq.com");       // 抄送们
        helper.setBcc("2513539439@qq.com");      // 密送们
        helper.setReplyTo("2513539439@qq.com");  // 回复
        javaMailSender.send(mimeMessage);
    }
}
